﻿using AutoMapper;
using MediatR;
using MyMES.Domain.zyy;
using MyMES.Error;
using MyMES.Infrastructure.Interface;
using MyMES.Writer.API.Application.Commends.Roles;

namespace MyMES.Writer.API.Application.CommendHandlers.Roles
{
    public class AddRoleHandler : IRequestHandler<AddRolecommand, APIResult<Role>>
    {
        private readonly IMapper mapper;
        private readonly IBaseRepository<Role> repository;

        public AddRoleHandler(IMapper mapper,IBaseRepository<Role> repository)
        {
            this.mapper = mapper;
            this.repository = repository;
        }

        public async Task<APIResult<Role>> Handle(AddRolecommand request, CancellationToken cancellationToken)
        {
            APIResult<Role> result = new APIResult<Role>();
            result.Code = APIEnum.添加成功;
            result.Msg = "添加成功";
            var model = mapper.Map<Role>(request);
            string sql = "insert into Role(RoleId,RoleName) values(@RoleId,@RoleName)";
            await repository.Insert(model, sql);
            return result;
        }
    }
}
